package com.pw.study.handler;

import com.alibaba.fastjson.JSONObject;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class KafkaHandler {
    private static Producer<String,String> producer;
    static {
        producer = getProducer();
    }

    private static Producer<String,String> getProducer() {
        Properties pro = new Properties();
        pro.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop112:9092");
        pro.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        pro.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        return new KafkaProducer<String,String>(pro);
    }

    public static void sendDataToKafka(String topic, String value) {
        producer.send(new ProducerRecord<String,String>(topic,value));
    }

    public static void main(String[] args) {

        System.out.println(producer);
    }
}
